using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public int health;
    public int damage;
    public SpriteRenderer sr;
    public float flashTime;
    public Color originalColor;

    public GameObject booldEffect;
    public GameObject damageNumber;
    // Start is called before the first frame update
    public void Start()
    {
        sr = GetComponent<SpriteRenderer>();
        originalColor = sr.color;
    }

    // Update is called once per frame
    public void Update()
    {
        if (health <= 0)
        {
            Destroy(transform.parent.gameObject);
        }
    }

    public void TakeDamage(int damage)
    {
        GameObject gbNumber = Instantiate(damageNumber, transform.position, Quaternion.identity);
        gbNumber.GetComponentInChildren<TextMesh>().text = damage.ToString();
        health -= damage;
        Flash(flashTime);
        Instantiate(booldEffect, transform.position, Quaternion.identity);
    }

    void Flash(float time)
    {
        sr.color = Color.red;
        Invoke("ResetColor", time);
    }
    void ResetColor()
    {
        sr.color = originalColor;
    }
}
